package com.wisdytech.ccdserver.matchwarning.serveice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wisdytech.ccdserver.matchwarning.dao.IWarningEquipmentDao;
import com.wisdytech.ccdserver.matchwarning.dao.IWarningMesSandstoneDao;
import com.wisdytech.ccdserver.matchwarning.model.WarningEquipment;
import com.wisdytech.ccdserver.matchwarning.model.WarningMesSandstone;
import com.wisdytech.ccdserver.matchwarning.serveice.IWarningEquipmentService;
import com.wisdytech.ccdserver.matchwarning.serveice.IWarningMesSandstoneService;
import com.wisdytech.common.bean.BaseModel;
import com.wisdytech.common.bean.BasePage;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.utils.BaseUtils;

import java.util.List;

/**
 * @author sunjinxin
 * @date 2024/5/20
 */
public class WarningMesSandstoneServiceImpl extends ServiceImpl<IWarningMesSandstoneDao, WarningMesSandstone> implements IWarningMesSandstoneService {
    @Override
    public IPage<WarningMesSandstone> getWarningMesSandstoneList(Filter filter) {

        Page<WarningMesSandstone> page = BasePage.getPage(filter);
        IPage<WarningMesSandstone> warningMesSandstoneIPage = baseMapper.getWarningMesSandstoneList(page,filter.getQueryMap());
        return  warningMesSandstoneIPage;
    }

    @Override
    public Boolean saveWarningMesSandstone(WarningMesSandstone warningMesSandstone, String username) {

        LambdaQueryWrapper<WarningMesSandstone> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarningMesSandstone::getEquipmentCode, warningMesSandstone.getEquipmentCode());
        queryWrapper.eq(BaseModel::getDelFlag, "0");
        List<WarningMesSandstone> warningMesSandstoneList = baseMapper.selectList(queryWrapper);

        if(warningMesSandstoneList.size() < 1){
            BaseUtils.setDefaultFiledsBaseModel(warningMesSandstone,username,true);
            baseMapper.insert(warningMesSandstone);
        }else {
            warningMesSandstone.setId(warningMesSandstoneList.get(0).getId());
            BaseUtils.setDefaultFiledsBaseModel(warningMesSandstone,username,false);
            baseMapper.updateById(warningMesSandstone);
        }
        return true;
    }
}
